package com.jiayang.hhotel.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.AuditItemAdapter;
import com.jiayang.hhotel.bean.AuditListModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.FinanceHandleModel;
import com.jiayang.hhotel.presenter.AuditItemPresenter;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XLazyFragment;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 查账子fragment 传递参数用于判断到底是来自那种item的请求
 */

public class AuditTabFragment extends XLazyFragment<AuditItemPresenter> {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private SwipeRefreshLayout fresh;
    private SwipeMenuRecyclerView recyclerView;
    private TextView total;//总计显示

    private int flag;

    private int page = 1;

    private AuditItemAdapter adapter;

    private FinanceHandleModel.PCashierListDtosBean model;

    public AuditTabFragment(int flag, FinanceHandleModel.PCashierListDtosBean model){
        this.flag = flag;
        this.model = model;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        layout = getRootView().findViewById(R.id.ll_data);
        icon = (ImageView) getRootView().findViewById(R.id.iv_pic);
        tv = (TextView) getRootView().findViewById(R.id.tv_text);

        total= (TextView) getRootView().findViewById(R.id.tv_consumption);
        fresh= (SwipeRefreshLayout) getRootView().findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) getRootView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        if(flag==1){
            total.setVisibility(View.GONE);
        }else{
            total.setVisibility(View.VISIBLE);
        }

        isLoadMore = true;
        isRefresh(true);
        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getGstAcct(),flag);
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page = 1;
                    getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,1,model.getGstAcct(),flag);
                }else {
                    isRefresh(false);
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    if (flag == 1 && !isLoadMore) {
                        isLoadMore = true;
                        page ++;
                        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getGstAcct(),flag);
                    }
                }
            }
        });
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isRefresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            fresh.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(Constant.NO_DATA_PICTURE);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(Constant.NET_ERROR_PICTURE);
                    tv.setText(getString(R.string.service_error_contract_manager));
                    icon.setOnClickListener(null);
                    break;
                default://其他错误
                    icon.setImageResource(Constant.TIME_OUT_PICTURE);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fresh.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isRefresh(true);
                            getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getGstAcct(),flag);
                        }
                    });
                    break;
            }
        }else{
            ToastUtil.Toast(StringUtil.errMsg(state),getActivity());
        }
    }

    private void isRefresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    public void setAdapter(List<AuditListModel.PTransDetailListDtosBean> model){
        isRefresh(false);
        if(adapter==null){
            adapter=new AuditItemAdapter(getActivity(),model,flag);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        if(flag!=1){
            total.setText(getString(R.string.total)+getString(R.string.space)+adapter.getTotalMoney());
        }
    }

    @Override
    public int getLayoutId() {
        switch (flag){
            case 1:
            case 4:
                return R.layout.fragment_audit_item;
            case 2:
            case 5:
                return R.layout.fragment_three_audit_item;
            case 3:
                return R.layout.fragment_two_audit_item;
        }
        return 0;
    }

    @Override
    public AuditItemPresenter newP() {
        return new AuditItemPresenter();
    }
}
